using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;


public class RingProcess : MonoBehaviour
{
    [Header("进度条速度")]
    public float speed = 1.0f;
    [Header("更新间隔")]
    public float update_interval = 0.1f;

    // 进度控制
    [Header("最大值")]
    public int targetProcess = 100;

    [Header("初始值")]
    public float currentAmout = 10;

    [Header("小数位数")]
    public int bar_decimal_place = 3;


    //一个图片一个文字
    Transform m_Image;
    Transform m_Text;
    TextMeshProUGUI comp_text;
    float last_time = -1f;

    private void Awake()
    {

        // BdUtils.log(" --- RingProcess.Awake");
        m_Image = gameObject.transform.Find("circle_out");
        m_Text = gameObject.transform.Find("text");
        comp_text = m_Text.GetComponent<TextMeshProUGUI>();

        // BdUtils.log(" --- m_Text:", m_Text.name, "--- m_Imag:", m_Image);
    }

    void set_cicle_progress_value()
    {
        string bar_format = $"0.{BdUtils.repeat_str("0", bar_decimal_place)}";
        comp_text.text = ((float)currentAmout).ToString(bar_format) + "%";
        m_Image.GetComponent<Image>().fillAmount = currentAmout / 100.0f;
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (currentAmout < targetProcess)
        {
            // speed = BdUtils.get_random_float(0f, 3f);

            if (Time.time - last_time < update_interval && last_time != -1f)
                return;
            last_time = Time.time;

            currentAmout += speed;
            if (currentAmout > targetProcess)
                currentAmout = targetProcess;

            set_cicle_progress_value();
        }
    }
}
